package cn.flying.cloud.system.log.service.impl;

import javax.annotation.Resource;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.system.log.entity.SysLoginLog;
import cn.flying.cloud.system.log.mapper.SysLoginLogMapper;
import cn.flying.cloud.system.log.service.SysLoginLogService;
import cn.flying.cloud.system.user.entity.SysUserInfo;
import cn.flying.cloud.system.user.service.SysUserInfoService;

/**
 * 登录日志表 服务实现类
 *
 * @author admin
 * @since 2025-08-17 22:06:46
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SysUserInfoService sysUserInfoService;

    /**
     * 查询总数
     *
     * @param param
     * @return
     */
    @Override
    public long countObj(SysLoginLog param) {
        return this.count(getLambdaQueryWrapper(param));
    }

    /**
     * 分页查询登录日志表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<SysLoginLog> getPageList(PageRequest<SysLoginLog> request) {
        SysLoginLog param = request.getParam();
        LambdaQueryWrapper<SysLoginLog> wrapper = getLambdaQueryWrapper(param);
        wrapper.orderByDesc(SysLoginLog::getCreatedTime);
        Page<SysLoginLog> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords());
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<SysLoginLog> getLambdaQueryWrapper(SysLoginLog param) {
        LambdaQueryWrapper<SysLoginLog> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            if (Ft.String.isNotBlank(param.getUserName())) {
                wrapper.eq(SysLoginLog::getUserName, param.getUserName());
            }
            if (Ft.String.isNotBlank(param.getUserId())) {
                wrapper.eq(SysLoginLog::getUserId, param.getUserId());
            }
            if (Ft.String.isNotBlank(param.getHappenType())) {
                wrapper.eq(SysLoginLog::getHappenType, param.getHappenType());
            }
        }
        return wrapper;
    }

    /**
     * 列表查询登录日志表数据
     *
     * @param param
     * @return
     */
    @Override
    public List<SysLoginLog> getList(SysLoginLog param) {
        LambdaQueryWrapper<SysLoginLog> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        return this.list(wrapper);
    }

    /**
     * 查询登录日志表详情数据
     *
     * @param param
     * @return
     */
    @Override
    public SysLoginLog getOneObj(SysLoginLog param) {
        LambdaQueryWrapper<SysLoginLog> wrapper = new LambdaQueryWrapper<>();
        // wrapper TODO
        return this.getOne(wrapper);
    }

    /**
     * 根据Id查询登录日志表详情数据
     *
     * @param id
     * @return
     */
    @Override
    public SysLoginLog getObjById(Serializable id) {
        return this.getById(id);
    }

    /**
     * 新增登录日志表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(SysLoginLog sysLoginLog) {
        if (Ft.String.isBlank(sysLoginLog.getUserId())) {
            SysUserInfo info = new SysUserInfo();
            info.setUsername(sysLoginLog.getUserName());
            info = sysUserInfoService.getOneObj(info);
            if (info != null) {
                sysLoginLog.setUserId(info.getUserId());
                sysLoginLog.setName(info.getName());
                sysLoginLog.setTenantId(info.getTenantId());
                sysLoginLog.setCreatedBy(info.getUsername());
                sysLoginLog.setUpdatedBy(info.getUsername());
            }
        }
        return this.save(sysLoginLog);
    }

    /**
     * 更新登录日志表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(SysLoginLog param) {
        return this.saveOrUpdate(param);
    }

    /**
     * 删除登录日志表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(SysLoginLog param) {
        LambdaQueryWrapper<SysLoginLog> wrapper = new LambdaQueryWrapper<>();
        // wrapper TODO
        return this.remove(wrapper);
    }

    /**
     * 根据Id删除登录日志表数据
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }
}
